<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="姓名" prop="name">
        <el-input maxlength="50"
                  class="search-width"
                  v-model="queryParams.name"
                  placeholder="请输入姓名"
                  clearable
                  @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input maxlength="50"
                  class="search-width"
                  v-model="queryParams.phone"
                  placeholder="请输入手机号"
                  clearable
                  @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input maxlength="50"
                  class="search-width"
                  v-model="queryParams.email"
                  placeholder="请输入邮箱"
                  clearable
                  @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['biz:bizExpert:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="Edit"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['biz:bizExpert:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['biz:bizExpert:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['biz:bizExpert:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="bizExpertList" border @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="left"/>
      <el-table-column label="序号" type="index" align="left" width="60"/>
      <el-table-column label="姓名" align="left" prop="name" min-width="120" show-overflow-tooltip/>
      <el-table-column label="手机号" align="left" prop="phone" min-width="120" show-overflow-tooltip/>
      <el-table-column label="邮箱" align="left" prop="email" show-overflow-tooltip/>
      <el-table-column label="创建人" align="left" prop="userName" width="120" show-overflow-tooltip/>
      <el-table-column label="创建时间" align="left" prop="createTime" width="180" show-overflow-tooltip/>
      <el-table-column fixed="right" label="操作" align="left" width="150">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['biz:bizExpert:edit']">修改
          </el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['biz:bizExpert:remove']">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <expertAdd ref="expertAddRef" :id="expertId" @ok="getList"/>
  </div>
</template>

<script setup name="BizExpert">
import {listBizExpert, delBizExpert} from "@/api/biz/bizExpert";
import {currentDate} from '@/utils/dateutil';
import expertAdd from "@/views/biz/bizExpert/expertAdd.vue";

const {proxy} = getCurrentInstance();

const bizExpertList = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const expertId = ref(0);

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null,
    phone: null,
    email: null,
    userId: null
  }
});

const {queryParams} = toRefs(data);

/** 查询专家列表 */
function getList() {
  loading.value = true;
  listBizExpert(queryParams.value).then(response => {
    bizExpertList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  expertId.value = null;
  nextTick(() => {
    proxy.$refs["expertAddRef"].show();
  });
}

/** 修改按钮操作 */
function handleUpdate(row) {
  expertId.value = row.id;
  nextTick(() => {
    proxy.$refs["expertAddRef"].show();
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除专家数据项？').then(function () {
    return delBizExpert(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('biz/bizExpert/export', {
    ...queryParams.value
  }, `专家-${currentDate()}.xlsx`)
}

getList();
</script>
